package com.smarth.zhizulian.utils.listhelper.impl.model;

import android.content.Context;

import com.smarth.zhizulian.bean.Page;
import com.smarth.zhizulian.http.observer.inter.IRefreshObserver;
import com.smarth.zhizulian.utils.listhelper.inter.IDataModel;
import com.smarth.zhizulian.utils.listhelper.inter.ResponseSender;

import io.reactivex.Observable;

/**
 * Created by 18865 on 2018/1/16.
 */

public abstract class HttpPageDataModel<T> implements IDataModel<T> {
    private Context mContext;

    public HttpPageDataModel(Context context) {
        mContext = context;
    }

    @Override
    public void refresh(ResponseSender<T> responseSender) {
        fetch(responseSender);
    }

    @Override
    public void loadMore(ResponseSender<T> responseSender) {
        fetch(responseSender);
    }

    @Override
    public boolean hasMore() {
        return true;
    }

    public void fetch(final ResponseSender<T> responseSender) {
        getObservable().subscribe(new IRefreshObserver<T>(mContext, responseSender) {
            @Override
            public void onNext(T t) {
                super.onNext(t);
                responseSender.sendData(t);
            }
        });
    }

    public abstract Observable<T> getObservable();
}
